﻿#region

using System;
using FluentValidation;
using FluentValidation.Attributes;
using HouseHoldBudget.Core.Infrastructure;

#endregion

namespace HouseHoldBudget.Infrastructure
{
    public class ValidatorFactory : AttributedValidatorFactory
    {
        #region Instance Methods

        public override IValidator GetValidator(Type type)
        {
            if (type != null)
            {
                var attribute = (ValidatorAttribute) Attribute.GetCustomAttribute(type, typeof (ValidatorAttribute));
                if ((attribute != null) && (attribute.ValidatorType != null))
                {
                    var instance = EngineContext.Current.ContainerManager.ResolveUnregistered(attribute.ValidatorType);
                    return instance as IValidator;
                }
            }
            return null;
        }

        #endregion
    }
}